<?php

namespace App\Repository\XhApi;

use App\Models\Xh\MemberModel;
use App\Repository\BaseRepository;

//会员
class MemberRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new MemberModel();
        parent::__construct();
    }

//    获取上级的推广者
    public function getIndirectId($spread_id)
    {
        if (empty($spread_id)) {
            return 0;
        }
        return $this->model->where('id', $spread_id)->value('indirect_id');
    }

    //    手机号注册一个新用户
    public function newFromMobile($mobile, $spread_id = 0, $share_id = 0)
    {
        $indirect_id = $this->getIndirectId($spread_id);
        $inData = [
            'mobile' => $mobile,
            'spread_id' => $spread_id,
            'indirect_id' => $indirect_id,
            'share_id' => $share_id
        ];
        return $this->model->insertGetId($inData);
    }

    //    微信新用户
    public function newFromMini($openid, $spread_id = 0, $share_id = 0)
    {
        $indirect_id = $this->getIndirectId($spread_id);
        $inData = [
//            'mobile' => '',
            'spread_id' => $spread_id,
            'indirect_id' => $indirect_id,
            'share_id' => $share_id,
            'mini_openid' => $openid
        ];
        return $this->model->insertGetId($inData);
    }
}
